/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.chanjet.donkey.rest;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chanjet.donkey.entity.User;
import com.chanjet.donkey.repository.UserDao;
import com.chanjet.donkey.service.tree.NestedSetService;

/**
 * Task的Restful API的Controller.
 * 
 * @author calvin
 */
@RestController
@RequestMapping(value = "/api/v1/user")
public class UserController extends BaseController<User> {
	@Autowired
	private UserDao userDao;
	@Autowired
	private NestedSetService nestsetService;
	
	public Page<User> index() {
//		Tree root = treeDao.findOne(1L);
//		Category root = new Category();
//		root.setName("News");
//		nestsetService.createRoot(root);
		
		
//		Category root = (Category) nestsetService.findById(Category.class, 1L);
//		
//		Category n1 = new Category();
//		n1.setName("NewsChild 1");
//		nestsetService.addChild(root, n1);
//		
//		Category n2 = new Category();
//		n2.setName("NewsChild 2");
//		nestsetService.addChild(root, n2);
//		
//		Category n3 = new Category();
//		n3.setName("NewsChild 3");
//		nestsetService.addChild(root, n3);
		
//		Tree news = new Tree();
//		news.setName("Sport News1");
//		treeService.appendTo(news, root);
		
//		Tree c1 = new Tree();
//		c1.setName("Sport News2");
//		treeService.appendTo(c1, root);
		
//		List<Tree> subtree = treeService.findRoots();
//		for (Tree node : subtree) {
//			System.out.println(node.getId());
//		}
		
//		List<Tree> roots = nestsetDao.findRoots();
//		for (Tree tree : roots) {
//			System.out.println(tree.getId());
//		}
//		Tree tree = new Tree();
//		tree.setName("tree1");
//		treeDao.save(tree);
//		
//		Tree child = new Tree();
//		child.setParent(tree);
//		child.setTree(tree);
//		child.setName("child1");
//		treeDao.save(child);
		
//		List<Tree> trees = (List<Tree>) treeDao.findAll();
//		for (Tree t : trees) {
//			System.out.println(t.getName() + ", parent_id: " + (t.getParent() != null ? t.getParent().getName() : 0) + ", tree_id: " + (t.getTree() != null ? t.getTree().getName() : 0));
//		}
		
		userDao.findAll();
		String name = param("name");
		String email = param("email");
		System.out.println(name + "=" + email);
		List<User> users = new ArrayList<User>();
		int page = getPage();
		long from = page == 1 ? 1 : 11;
		long to = page == 1 ? 10 : 15;
		for (long i = from; i <= to; i++) {
			User user = new User();
			user.setId(i);
			user.setLoginName("u" + i); 
			user.setName("name-" + i);
			
//			userDao.save(user);
			
			if (name == null || name.equals("") || user.getName().contains(name)) {
				users.add(user);
			}
		}
		return new PageImpl<User>(users, new QPageRequest(page, 10), 15);
	}

	public User show(Long id) {
		User user = new User();
		user.setId(1L);
		user.setLoginName("u" + 1);
		user.setName("name-" + 1);
		
		return user;
	}

	
	
}
